package com.example.visitsweb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.visitsweb.entity.Verify;
import com.example.visitsweb.lang.Result;
import com.example.visitsweb.mapper.VerifyMapper;
import com.example.visitsweb.service.VerifyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.visitsweb.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author MarkerHub
 * @since 2022-04-26
 */
@Service
public class VerifyServiceImpl extends ServiceImpl<VerifyMapper, Verify> implements VerifyService {
@Autowired
VerifyMapper verifyMapper;
@Autowired
    RedisUtil redisUtil;

    @Override
    public boolean Record(String phone,String code) {
        Verify verify1 = verifyMapper.selectOne(new QueryWrapper<Verify>().eq("phone",phone).eq("verify_code", code));
       if (verify1!=null){
           return true;
       }
        return false;
    }

    @Override
    public Result getCode(String phone) {
        if(redisUtil.hasKey(phone)){
            return Result.succ(redisUtil.get(phone));
        }
        Verify verify = verifyMapper.selectOne(new QueryWrapper<Verify>().eq("phone", phone));
        return Result.succ(verify.getVerifyCode());
    }
}
